package common_classes;

import java.awt.AWTException;
import java.awt.Font;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import main_classes.Server;


public class Tray {
	private MenuItem startItem, restartItem, stopItem, exitItem;
	private boolean enabledStartItem;
	private SystemTray tray;
	private TrayIcon trayIcon; 

	public Tray() {
		if (!SystemTray.isSupported()) {
			System.err.println("SystemTray is not supported");
			return;
		}

		final PopupMenu popup = new PopupMenu();
		Image icon = Toolkit.getDefaultToolkit().getImage("start.png");

		trayIcon = new TrayIcon(icon);
		tray = SystemTray.getSystemTray();

		startItem = new MenuItem("Start");
		startItem.setFont(new Font("Verdana", Font.BOLD + Font.ITALIC, 12));
		enabledStartItem = true;
		restartItem = new MenuItem("Restart");
		restartItem.setFont(new Font("Verdana", Font.BOLD + Font.ITALIC, 12));
		stopItem = new MenuItem("Stop");
		stopItem.setFont(new Font("Verdana", Font.BOLD + Font.ITALIC, 12));
		exitItem = new MenuItem("Exit");
		exitItem.setFont(new Font("Verdana", Font.BOLD, 12));

		restartItem.setEnabled(false);
		stopItem.setEnabled(false);

		if (enabledStartItem) {
			trayIcon.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					sendMsg("START");
					restartItem.setEnabled(true);
					stopItem.setEnabled(true);
					startItem.setEnabled(false);
					enabledStartItem = false;
				}
			});
		}

		startItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				sendMsg("START");
				restartItem.setEnabled(true);
				stopItem.setEnabled(true);
				startItem.setEnabled(false);
				enabledStartItem = false;
			}
		});

		restartItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				sendMsg("RESTART");
				restartItem.setEnabled(true);
				stopItem.setEnabled(true);
				startItem.setEnabled(false);
				enabledStartItem = false;
			}
		});

		stopItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				sendMsg("STOP");
				restartItem.setEnabled(false);
				stopItem.setEnabled(false);
				startItem.setEnabled(true);
				enabledStartItem = true;
			}
		});

		popup.add(startItem);
		popup.add(restartItem);
		popup.add(stopItem);
		popup.addSeparator();

		trayIcon.setPopupMenu(popup);

		try {
			tray.add(trayIcon);
		} catch (AWTException e) {
			System.err.println("TrayIcon could not be added.");
		}
	}
	
	public void sendMsg(String msg) {
		try {
			Socket socket = new Socket("localhost", Server.getSettings().getPort());
			OutputStream os = socket.getOutputStream();
			os.write(("SERVER\r\n" + Server.getAdminName() + "\r\n" + Server.getAdminPassword() + "\r\n" + msg + "\r\n").getBytes());
			os.flush();
			socket.close();
		} catch (IOException e) {
			System.err.println(e);
		}
	}

	public void removeIcon() {
		tray.remove(trayIcon);
	}
}